package org.jxc.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.SalesOrderProductDAO;
import org.jxc.domain.SalesOrderProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.SearchUtil;
import org.team4.vo.Page;

@Service
public class SalesOrderProductService implements IService {

	private SalesOrderProductDAO salesOrderProductDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		salesOrderProductDAO = (SalesOrderProductDAO) DAOFactory.getDAO(
				sessionFactory, SalesOrderProductDAO.class);
	}

	public Page<SalesOrderProduct> listPagination(Page<SalesOrderProduct> page,
			String searchType, List<String> searchValue) throws CRUDException {
		String ss = SearchUtil
				.getString(new String[] { "product.name",
						"salesOrder.orderDate" },// 高级查询条件
						new String[] { SearchUtil.STRING_LIKE,
								SearchUtil.BETWEEN_DATE },// 查询类型
						searchType,// 与或类型
						searchValue);// 查询值
		return salesOrderProductDAO.pagination(page,
				"from SalesOrderProduct where salesOrder.outState=1 " + ss, "product.id");
	}

	/**
	 * 根据订单ID获取隶属于其的销售订单明细
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<SalesOrderProduct> findAllSalesOrderProductBySalesOrderId(
			String salesOrderId) throws CRUDException {
		List<SalesOrderProduct> list = salesOrderProductDAO.find(
				"from SalesOrderProduct where salesOrder.id = ?", salesOrderId);
		return list;
	}

	/**
	 * 根据id查询采购订单明细
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public SalesOrderProduct findById(String id) throws Exception {
		SalesOrderProduct salesOrderProduct = salesOrderProductDAO.get(id);
		return salesOrderProduct;
	}

	/**
	 * 新建采购订单明细
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void saveSalesOrderProduct(SalesOrderProduct salesOrderProduct)
			throws CRUDException {
		salesOrderProductDAO.save(salesOrderProduct);
	}

	/**
	 * 修改采购订单明细
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void updateSalesOrderProduct(SalesOrderProduct salesOrderProduct)
			throws CRUDException {
		SalesOrderProduct old = salesOrderProductDAO.get(salesOrderProduct
				.getId());

		old.setProduct(salesOrderProduct.getProduct());
		old.setAmounts(salesOrderProduct.getAmounts());
		old.setDiscount(salesOrderProduct.getDiscount());
		old.setLastPrice(salesOrderProduct.getLastPrice());
		old.setPrice(salesOrderProduct.getPrice());
		old.setSalesOrder(salesOrderProduct.getSalesOrder());
		old.setSalesPrice(salesOrderProduct.getSalesPrice());
		old.setStock(salesOrderProduct.getStock());
		old.setRemark(salesOrderProduct.getRemark());
		old.setTax(salesOrderProduct.getTax());
		salesOrderProductDAO.update(old);
	}

	public void deleteSalesOrderProductBySalesOrderId(String salesOrderOrderId)
			throws CRUDException {
		List<SalesOrderProduct> list = findAllSalesOrderProductBySalesOrderId(salesOrderOrderId);
		for (SalesOrderProduct sop : list) {
			salesOrderProductDAO.delete(sop);
		}
	}

	/**
	 * 批量删除商品
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteSalesOrderProduct(List<String> idList)
			throws CRUDException {
		salesOrderProductDAO.deleteByIdList(idList);
	}

}